package com.tanxi521.afkreward.listener;

import com.tanxi521.afkreward.data.AFKRewardMapper;
import com.tanxi521.afkreward.pojo.Area;
import com.tanxi521.afkreward.pojo.PlayerAfk;
import com.tanxi521.afkreward.util.ColorUtil;
import com.tanxi521.afkreward.util.ConfigUtil;
import com.tanxi521.afkreward.util.GraphUtils;
import com.tanxi521.afkreward.util.MapPoint;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.event.inventory.InventoryOpenEvent;
import org.bukkit.event.player.*;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.stream.Collectors;

public class AfkListener implements Listener {

    @EventHandler
    public void onPlayerJoin(PlayerJoinEvent e) {
        PlayerAfk player = new PlayerAfk();
        player.setPlayerName(e.getPlayer().getName());
        player.setPlayerUuid(e.getPlayer().getUniqueId().toString());
        player.setType("未挂机");
        player.setReward("未挂机");
        player.setStart(0L);
        player.setEnd(0L);
        player.setTimes(0L);
        player.setTotal(0L);
        AFKRewardMapper.insertPlayer(player);
    }

    @EventHandler
    public void onMove(PlayerMoveEvent e) {
        if (AFKRewardMapper.AFK_MAP.containsKey(e.getPlayer().getName())) {
            PlayerAfk playerAfk = AFKRewardMapper.AFK_MAP.get(e.getPlayer().getName());
            if (playerAfk.getType().equals("自由")) {
                if (e.getFrom().distance(e.getTo()) != 0) {
                    e.setCancelled(true);
                }
            } else {
                double x = e.getTo().getX();
                double z = e.getTo().getZ();
                MapPoint mapPoint = new MapPoint();
                mapPoint.setLng(x);
                mapPoint.setLat(z);
                Area area = ConfigUtil.areas.stream().filter(a -> playerAfk.getReward().equals(a.getName())).collect(Collectors.toList()).get(0);
                if (!GraphUtils.isPointInPolygon(mapPoint, area.getMapPoints())) {
                    e.getPlayer().sendMessage(ColorUtil.replace(ConfigUtil.langYml.getString("dontmove")));
                    e.setCancelled(true);
                }
            }
        }
    }

    @EventHandler
    public void onPlayerInteract(PlayerInteractEvent e) {
        if (AFKRewardMapper.AFK_MAP.containsKey(e.getPlayer().getName())) {
            e.setCancelled(true);
            e.getPlayer().sendMessage(ColorUtil.replace(ConfigUtil.langYml.getString("afkwarning")));
        }
    }

    @EventHandler
    public void onHurt(EntityDamageEvent e) {
        if (e.getEntity() instanceof Player) {
            if (AFKRewardMapper.AFK_MAP.containsKey(e.getEntity().getName())) {
                e.setCancelled(true);
                e.getEntity().sendMessage(ColorUtil.replace(ConfigUtil.langYml.getString("afkwarning")));
            }
        }
    }

    @EventHandler
    public void noBreak(BlockBreakEvent e) {
        if (AFKRewardMapper.AFK_MAP.containsKey(e.getPlayer().getName())) {
            e.setCancelled(true);
            e.getPlayer().sendMessage(ColorUtil.replace(ConfigUtil.langYml.getString("afkwarning")));
        }
    }

    @EventHandler
    public void noInventory(InventoryOpenEvent e) {
        if (AFKRewardMapper.AFK_MAP.containsKey(e.getPlayer().getName())) {
            e.setCancelled(true);
            e.getPlayer().sendMessage(ColorUtil.replace(ConfigUtil.langYml.getString("afkwarning")));
        }
    }

    @EventHandler
    public void noTeleport(PlayerTeleportEvent e) {
        if (AFKRewardMapper.AFK_MAP.containsKey(e.getPlayer().getName())) {
            e.setCancelled(true);
            e.getPlayer().sendMessage(ColorUtil.replace(ConfigUtil.langYml.getString("afkwarning")));
        }
    }

    @EventHandler
    public void onPlayerExit(PlayerQuitEvent e) {
        PlayerAfk playerAfk = AFKRewardMapper.AFK_MAP.get(e.getPlayer().getName());
        if (playerAfk != null) AFKRewardMapper.updateExitPlayerEnd(e.getPlayer().getName());
    }

}
